package uw.illno.controller

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import uw.illno.pojo.Student
import uw.illno.service.AdministratorService

@Controller
class InsertStudent {
    @Autowired
    private lateinit var service: AdministratorService

    @RequestMapping(value = ["/insertstu"], method = [RequestMethod.POST])
    fun sinsert(
        sno: String,
        sname: String,
        sex: String,
        phone: String,
        sclass: String,
        age: Int,
        model: Model
    ): String {
//
        return if (sname != "" && phone.length == 11 && sno != "" && sclass != "" && phone != "" && age >= 10 && age <= 100 && sex != "") {
            val student = Student(sname, sno, sclass, phone, age, "123456", sex)
            val r = service.student_insert(student)
            if (r > 0) {
                model.addAttribute("msgi1", "添加成功！")
                "adpages/adminindex"
            } else {
                model.addAttribute("msgi", "添加学生失败！")
                "adpages/adminindex"
            }
        } else {
            model.addAttribute("msgi", "输入信息有误或有空信息！")
            "adpages/adminindex"
        }
    }
}
